package com.cvit.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cvit.dao.OrdersMapper;
import com.cvit.dao.StudentMapper;
import com.cvit.model.Orders;
import com.cvit.model.Student;
import com.cvit.utils.Utils;

@Service
public class OrdersService {
	
	@Autowired
	OrdersMapper ordersMapper;
	@Autowired
	StudentMapper studentMapper;

	/**
	 * 根据身份证号或者学生号查询订单表
	 * */
	public Orders getOrdersById(String numberId) {
		String id = "";
		if(numberId.length()==18){
			Student student = studentMapper.selectByCardId(numberId);
			if(Utils.isNull(student)==false){
				id=student.getId();
			}
		}else{
			Student student = studentMapper.selectByEamineeId(numberId);
			if(Utils.isNull(student)==false){
				id=student.getId();
			}
		}
		return ordersMapper.selectById(id);
	}
	
	/**
	 * 添加订单信息
	 * */
	public String addOrders(Orders orders) {
		orders.setTotalFee(200);
		orders.setType(2);
		ordersMapper.insert(orders);
		return orders.getId();
	}

	/**
	 * 修改订单信息
	 * */
	public void updateOrders(Orders orders) {
		ordersMapper.updateByPrimaryKeySelective(orders);
	}

	public List<Orders> getAll() {
		List<Orders> orders = ordersMapper.selectByExample(null);
		System.out.println("==========="+orders);
		return orders;
	}
	
	/**
	 * 根据订单号查询学生序号
	 * */
	public String selectStudentIdById(String id){
		String studentId = ordersMapper.selectStudentIdById(id);
		return studentId;
	}
	
	/**
	 * Id判重
	 * */
	public String JudgeIdRepeat(){
		String id=null;
		int i=0;
		for(;i==0;){
			String orderNo = "04";
			SimpleDateFormat sdf=new SimpleDateFormat("yyMMdd");
			String nowTime = sdf.format(new Date())+Utils.get6Radom();
			orderNo+=nowTime;
			id = orderNo;
			if(ordersMapper.selectByPrimaryKey(id)==null){
				i=1;
			}
		}
		return id;
	}

	/**
	 * 根据订单号查询订单状态
	 * */
	public int selectTypeById(String id) {
		return ordersMapper.selectTypeById(id);
	}

	/**
	 * 根据订单号查询订单
	 * */
	public Orders getOrdersByOrdersId(String id) {
		return ordersMapper.selectByPrimaryKey(id);
	}

	
	public int getOrdersByStudentId(String id) {
		// TODO Auto-generated method stub
		Orders orders = ordersMapper.selcetByStudentId(id);
		Utils.isNull(orders);
		if(Utils.isNull(orders)==false)
		return 1;
		else
		return 2;
	}
	
}
